/**
 * 产品修改js
 */
var comValidform = null;
var returnStatus = false;
var points = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	pointsUE : null,

	startEvent : function() {
		$("#recordTab ul").idTabs();

		comValidform = $(".com-addForm").Validform({
			datatype : {
				"m0-2" : /^[0-9]+(.[0-9]{1,2})?$/
			},
			tiptype : 2,
			tipSweep : true,
			beforeSubmit : function(curform) {
				var url = 'mgr/pointsCtrl/updatepointsInfo';
				var params = jsUtils.serializeJson($('.com-addForm'));

				var pointsText = points.pointsUE.getContent();
				params.points = pointsText;

				var cb = function(data) {
					alert(data.msg);
					// 成功返回
					if (data.resultCode == 200) {
						// 关闭窗口并刷新数据
						art.dialog.close();
						returnStatus = true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/pointsCtrl/list';
		var typeName = $.trim($('#searchModForm input[name="typeName"]').val());
		var params = {
			'curPage' : points.curPage,
			'pageSize' : points.pageSize,
			'typeName' : typeName,
		};

		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / points.pageSize > 0) ? Math
						.ceil(totalCount / points.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, points.search,
						delCurPage); // 翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	//查询积分规则
	search : function(pageIdx) {
		var url = 'mgr/pointsCtrl/list';
		if (typeof (pageIdx) != "undefined") {
			points.curPage = pageIdx;
		}
		var typeName = $.trim($('#searchModForm input[name="typeName"]').val());
		var params = {
			'curPage' : points.curPage,
			'pageSize' : points.pageSize,
			'typeName' : typeName
		};
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';
			if (data.resultCode == 200) {
				if (data.result != null && data.result.length > 0) {
					for (var i = 0; i < data.result.length; i++) {
						var singleObj = data.result[i];
						var orderCreatedOn = getSmpFormatDateByLong(
								singleObj.cts, true);
						localStorage.setItem(singleObj.id + "_points_Key",
								JSON.stringify(singleObj));

						tr += '<tr><td><input type="hidden" name="id" value="'
								+ singleObj.id
								+ '"/>'
								+ singleObj.id
								+ '</td><td>'
								+  singleObj.typeName
								+ '</td><td>'
								+ (singleObj.scoreType == 1 ? "+":"-")+singleObj.score
								+ '</td><td>'
								+  singleObj.maxDayTimes
								+ '</td><td>'
								+ (singleObj.actDesc != null?singleObj.actDesc:"")
								+ '</td><td>'
								+ (singleObj.status == 1 ? "已生效":"未生效")
								+ '</td><td>'
								+ (singleObj.creatorName!= null?singleObj.creatorName:"")
								+ '</td><td>'
								+ orderCreatedOn
								+ '</td><td>'
								+ '<a type="button" name="points_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;">'
								+ '<i class="fa fa-edit"></i> 编辑</a>'
								+ '<a type="button" name="points_91" class="btn btn-success btn-xs updateStatus" style="margin-left: 5px;">'
								+ '<i class="fa fa-edit"></i>更改状态</a>'
								+ '</td></tr>';
					}
				} else {
					tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
			} else {
				tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			jsUtils.controlFunctionBtn();
			points.bindEvent();
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			points.curPage = 1;
			points.setPageChange(null);
			points.search();
		});
		$('.updateData').unbind();
		$('.updateData').on('click',
			function() {
			var thisObj = $(this);
			var id = thisObj.parent().parent().find(
					'input[name="id"]').eq(0).val();
			art.dialog.data('updId', id);

			var path = APP_ADDRESS
					+ 'mgr/activity/points/pointsUpdate.jsp';
			art.dialog
					.open(
							path,
							{
								title : '修改积分规则',
								width : 550,
								height : 250,
								ok : function() {
									var returnStatus = this.iframe.contentWindow
											.submitForm();
									if (returnStatus) {
										points
												.setPageChange(points.curPage);
										points
												.search(points.curPage);
									}
									return false;
								},
								cancel : true
							});
		});
		$('.updateStatus').unbind();
		$('.updateStatus').on('click',
			function() {
			var thisObj = $(this);
			var id = thisObj.parent().parent().find(
					'input[name="id"]').eq(0).val();
			art.dialog.data('updId', id);

			var path = APP_ADDRESS
					+ 'mgr/activity/points/pointsUpdateStatus.jsp';
			art.dialog
					.open(
							path,
							{
								title : '修改积分规则发布状态',
								width : 550,
								height : 250,
								ok : function() {
									var returnStatus = this.iframe.contentWindow
											.submitForm();
									if (returnStatus) {
										points
												.setPageChange(points.curPage);
										points
												.search(points.curPage);
									}
									return false;
								},
								cancel : true
							});
		});
		$('#addData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/activity/points/pointsAdd.jsp';
			art.dialog.open(path, {
				title : '添加积分规则',
				width : 550,
				height : 250,
				ok : function() {
					var returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						points.setPageChange(null);
						points.search();
					}
					return false;
				},
				cancel : true
			});
		});

		 
		$('.lookData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('lokId', id);
					var path = APP_ADDRESS + 'mgr/other/points/pointsLook.jsp';
					art.dialog.open(path, {

						title : '查看协议',
						width : 600,
						height : 300,
						cancel : true
					});
				});

	},
	showInfoById : function() {
		var updId = art.dialog.data('updId'); // 修改id
		var url = 'mgr/pointsCtrl/getAgrrementInfoById';
		var params = {
			'id' : updId
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var ret = data.result;

				if (ret.pointsTxt != null) {
					points.pointsUE.ready(function() {
						points.pointsUE.setContent(ret.riskctrlTxt);
					});
				}
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	init : function() {
		points.setPageChange(null);
		points.search();
		points.startEvent();
	}
}
$(document).ready(function() {
	points.init();
});
function submitForm() {
	comValidform.submitForm();
	return returnStatus;
}